#include <iostream>
#include <vector>
#include <csignal>

#include "../comm/httplib.h"
#include "../comm/util.hpp"
#include "../comm/log.hpp"
#include "main_server_control.hpp"

using namespace httplib;
using namespace ns_control;

void signal_handler(int signal)
{
        if (signal == SIGTERM)
        {
                std::exit(0);
        }
}

int main()
{

        std::signal(SIGTERM, signal_handler);
        Server svr;

        control ctrl;

        // 业务1. 首页展示
        svr.Get("/", [&ctrl](const Request &req, Response &resp)
                {
                std::string html;
                ctrl.getIndex(&html);
                resp.set_content(html, "text/html;charset=utf8"); });

        // 业务2. 获取全部作品
        svr.Get("/books/all", [&ctrl](const Request &req, Response &resp)
                { resp.set_redirect("/books/all/page/1"); });

        svr.Get(R"(/books/all/page/(\d+))", [&ctrl](const Request &req, Response &resp)
                { 
                // /books/pageX/%d
                std::string html;
                std::string page = req.matches[1];
                ctrl.allBooks(page, &html);
                resp.set_content(html, "text/html; charset=utf-8"); });

        // 业务3. 首页获取分类作品
        svr.Get(R"(/books/type/(\w+)/page/(\d+))", [&ctrl](const Request &req, Response &resp)
                {
                 std::string html;
                 std::string type = req.matches[1];
                 std::string page = req.matches[2];
                 ctrl.typeOfBooks(type, page, &html);
                 resp.set_content(html, "text/html; charset=utf-8"); });

        // 业务4. 登录界面
        svr.Get("/login", [&ctrl](const Request &req, Response &resp)
                { 
                std::string html;
                ctrl.login(&html);
                resp.set_content(html, "text/html; charset=utf-8"); });

        // 业务5. 登录提交
        svr.Post("/loginSubmit", [&ctrl](const Request &req, Response &resp)
                 {
                std::string result;
                ctrl.loginSumbit(req.body, &result);
                resp.set_content(result, "application/json; charset=utf-8"); });

        // 登录成功
        svr.Get(R"(/loginSuccess/(\d+))", [&ctrl](const Request &req, Response &resp)
                { 
                std::string userid = req.matches[1];
                std::string html;
                ctrl.loginSuccess(userid, &html);
                resp.set_content(html, "text/html; charset=utf-8"); });

        // 业务6. 注册
        svr.Post("/register", [&ctrl](const Request &req, Response &resp)
                 { 
                std::string result;
                ctrl.registerUser(req.body,&result);
                resp.set_content(result, "application/json; charset=utf-8"); });

        // 出错处理
        svr.Get("/none", [&ctrl](const Request &req, Response &resp)
                { 
                std::string html;
                ctrl.none(&html);
                resp.set_content(html, "text/html; charset=utf-8"); });

        // 业务7. 搜索

        svr.listen("0.0.0.0", 8080);
        return 0;
}